package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.fastjson.JSONObject;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.Bean;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.model.entity.ParentAreaMeasureEntity;
import com.sgb.kjwl.model.entity.enterprise.ServiceProjectClassEntity;
import com.sgb.kjwl.model.entity.enterprise.TaxscaleEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PublishServiceActivityModel extends AndroidViewModel {

    public MutableLiveData<List<ParentAreaMeasureEntity<List<ParentAreaMeasureEntity<List<ParentAreaMeasureEntity>>>>>> ldServiceType = new MutableLiveData<>();
    public MutableLiveData<String> ldSaveSuccess = new MutableLiveData<>();
    public MutableLiveData<String> ldUpdateSuccess = new MutableLiveData<>();
    public MutableLiveData<Boolean> show_project_type = new MutableLiveData<>(false);//服务项目，fals为默认
    public MutableLiveData<String> toast = new MutableLiveData<>();
    public List<Bean> taxt_project_type  = new ArrayList<>();


    public List<ServiceProjectClassEntity> listdata = new ArrayList<>();
    public boolean isupdateProjectClass = false;//是否修改了分类

    public List<Map<String,Object>> pricelist = new ArrayList<>();//价格数据
    public String maxPrice = "";
    public String minPrice = "";

    public PublishServiceActivityModel(@NonNull Application application) {
        super(application);
        if(taxt_project_type.size()==0){
            taxt_project_type.add(new Bean("默认服务项目"));
            taxt_project_type.add(new Bean("自定义服务项目"));
        }
    }


    /**
     * 获取服务类别
     */
    public void getServiceType(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getServiceCategoryMeasureOption().enqueue(new Callback<BaseEntity<List<ParentAreaMeasureEntity<List<ParentAreaMeasureEntity<List<ParentAreaMeasureEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ParentAreaMeasureEntity<List<ParentAreaMeasureEntity<List<ParentAreaMeasureEntity>>>>>>> call,
                                   Response<BaseEntity<List<ParentAreaMeasureEntity<List<ParentAreaMeasureEntity<List<ParentAreaMeasureEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(context, "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldServiceType.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ParentAreaMeasureEntity<List<ParentAreaMeasureEntity<List<ParentAreaMeasureEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 发布服务
     */
    public void saveService(Context context, Map<String, Object> map) {
        map.put("serviceStatus",1);//上架
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);

        NewAPIManager.getInstance().getEnterPrisenewAPI().saveService(map).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null ) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldSaveSuccess.postValue(response.body().getData());
                    toast.postValue("操作成功");
                } else {
                    MToast.showToast(context, response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 更新服务
     */
    public void updateService(Context context, Map<String, Object> map) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);

        NewAPIManager.getInstance().getEnterPrisenewAPI().updateService(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null ) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldUpdateSuccess.postValue("ok");
                    toast.postValue("操作成功");
                } else {
                    MToast.showToast(context, response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 递归组装数据
     * @param map_list 缓存键
     * @param child 组装键
     * @return
     */
    public List<Map<String,Object>> combinationData(List<Map<String,Object>> map_list,List<ServiceProjectClassEntity.ChildBean> child,int position){
        List<Map<String,Object>> new_list = new ArrayList<>();
        if(map_list.size()>0){
            for (int i = 0; i < map_list.size(); i++) {
                String name = map_list.get(i).get("name")+"";
                for (ServiceProjectClassEntity.ChildBean childBean : child) {
                    Map<String,Object> data = JSONObject.parseObject(JSONObject.toJSONString(map_list.get(i)), HashMap.class);
                    data.put("type_no_"+position,childBean.getNo());
                    data.put("type_name_"+position,childBean.getName());
                    data.put("name",name+"/"+childBean.getName());
                    new_list.add(data);
                }
            }
        }else{
            for (ServiceProjectClassEntity.ChildBean childBean : child) {
                Map<String,Object> data = new HashMap<>();
                data.put("type_no_"+position,childBean.getNo());
                data.put("type_name_"+position,childBean.getName());
                data.put("name",childBean.getName());
                new_list.add(data);
            }
        }
        return new_list;
    }

}
